package com.cems.service.impl;

import com.cems.mapper.AdminMapper;
import com.cems.pojo.*;
import com.cems.service.AdminService;
import com.cems.util.RedisUtil;
import com.cems.util.SelectRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName AdminServiceImpl
 * @Author 陈新予(blank)
 * @Date 2021/5/16
 * @Version 1.0
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    AdminMapper adminMapper;

    @Override
    public List<SysAdmin> getAdmins() {
        try {
            //调用redis查询方法
            List<Object> objects = SelectRedis.selectRedis(redisUtil, "cems_AllAdmins", adminMapper, "getAdmins");
            ArrayList<SysAdmin> admins = new ArrayList<>();
            for (Object object : objects) {
                if (object instanceof SysAdmin) {
                    admins.add((SysAdmin) object);
                }
            }
            return admins;
        } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            e.printStackTrace();
            return adminMapper.getAdmins();
        }
    }

    @Override
    public void addAdmin(HashMap<String, String> map, String num, String emil, String phone) {
        //添加新注册的用户
        adminMapper.addAdmins(map);
        //添加用户的信息
        int adminIdByNum = adminMapper.getAdminIdByNum(num);
        adminMapper.addAdminEmilAndPhone(adminIdByNum, emil, phone);
        redisUtil.delete("cems_AllAdmins");
    }

    @Override
    public List<SysAdminInfo> getAllAdminInfo() {
        try {
            //调用redis查询方法
            List<Object> objects = SelectRedis.selectRedis(redisUtil, "cems_AllAdminInfo", adminMapper, "getAllAdminInfo");
            ArrayList<SysAdminInfo> admins = new ArrayList<>();
            for (Object object : objects) {
                if (object instanceof SysAdminInfo)
                    admins.add((SysAdminInfo) object);
            }
            return admins;
        } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            e.printStackTrace();
            return adminMapper.getAllAdminInfo();
        }
    }

    @Override
    public void updateAdminLevel(String id, String author) {
        //更改权限
        adminMapper.updateAdminLevel(id, author);
        //更新redis
        redisUtil.delete("cems_AllAdmins");
        redisUtil.delete("cems_AllAdminInfo");
    }

    @Override
    public void updateAdminStatus(String id, String status) {
        adminMapper.updateAdminStatus(id,status);
        //更新redis
        redisUtil.delete("cems_AllAdmins");
        redisUtil.delete("cems_AllAdminInfo");
    }

    @Override
    public void addAdminUpgrade(HashMap<String, String> map) {
        adminMapper.addAdminUpgrade(map);
    }

    @Override
    public List<SysAdminUpgrade> getAdminUpgrade() {
        return adminMapper.getAdminUpgrade();//小数据不经过redis
    }

    @Override
    public SysAdminInfo getAdminInfo(Integer adminId) {
        return adminMapper.getAdminInfo(adminId);
    }

    @Override
    public void changePassword(String newPwd, Integer adminId) {
        adminMapper.changePassword(newPwd,adminId);
        redisUtil.delete("cems_AllAdmins");
    }

    @Override
    public void updateAdminInfo(SysAdminInfo admin) {
        adminMapper.updateAdminInfo(admin);
        redisUtil.delete("cems_AllAdminInfo");
    }

    @Override
    public List<UserShensu> findShensuList(){
        return adminMapper.findShensuList();
    }


    //解封
    @Override
    public void jiefengUser(String phone){
        adminMapper.jiefengUser(phone);
        adminMapper.deleteShensu(phone);
        redisUtil.delete("cems_UserMessages");
        redisUtil.delete("cems_AllUsers");
    }

    @Override
    public String findStatus(String adminNum) {
        return adminMapper.findStatus(adminNum);
    }

    @Override
    public void adminShensu(String adminNum, String content) {
        adminMapper.adminShensu(adminNum,content);
    }

    @Override
    public List<AdminShensu> findAdminShensuList() {
        return adminMapper.findAdminShensuList();
    }


    @Override
    public void jiefengAdmin(String adminNum) {
        adminMapper.jiefengAdmin(adminNum);
        adminMapper.deleteShensuAdmin(adminNum);
        redisUtil.delete("cems_AllAdmins");
        redisUtil.delete("cems_AllAdminInfo");
    }

    @Override
    public SysAdmin selOneSysAdmin(String adminNum) {
        return adminMapper.selOneSysAdmin(adminNum);
    }

    @Override
    public void zhaohuiAdmin(String adminNum, String newpwd) {
        adminMapper.zhaohuiAdmin(adminNum, newpwd);
        redisUtil.delete("cems_AllAdminInfo");
        redisUtil.delete("cems_AllAdmins");
    }


    @Override
    public int zhaohuicheck(String adminNum, String email) {
        return adminMapper.zhaohuicheck(adminNum,email);
    }
}
